package com.example.robertluozizhao.framecollectdemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Scroller;

import com.example.robertluozizhao.framecollectdemo.R;

/**
 * Created by robert.luozizhao on 2018/1/11.
 */

public class SlideMenuViewGroup extends ViewGroup {

    private View leftChildAt;
    private View contentChildAt;
    private float moveX;
    private float downX;
    private Context mContext;
    private Scroller mScroller;
    private boolean slideMenuState = false;

    public ImageView getImageButton() {
        return mImageButton;
    }

    public void setImageButton(ImageView imageButton) {
        mImageButton = imageButton;
    }

    private ImageView mImageButton;

    public SlideMenuViewGroup(Context context) {
        super(context, null);
    }

    public SlideMenuViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
//        initView();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LayoutParams leftlayoutParams = leftChildAt.getLayoutParams();
        int leftRealWidth = MeasureSpec.makeMeasureSpec(leftlayoutParams.width, MeasureSpec.EXACTLY);
        leftChildAt.measure(leftRealWidth, heightMeasureSpec);
        contentChildAt.measure(widthMeasureSpec, heightMeasureSpec);//没有这句话 下面layout么有效果
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        leftChildAt.layout(-leftChildAt.getMeasuredWidth(), 0, 0, leftChildAt.getMeasuredHeight());
        contentChildAt.layout(0, 0, contentChildAt.getMeasuredWidth(), contentChildAt.getMeasuredHeight());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX() + 0.5f;
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = event.getX() + 0.5f;
                float distanceX = downX - moveX;
                Log.d("diy", "onTouchEvent: downX:" + downX + ",moveX:" + moveX);
                int scrollX = getScrollX();// https://www.cnblogs.com/xingyi7/articles/4520768.html  屏幕 左上角与（当前view）contentChildAt的x 偏移。
                if (scrollX + distanceX < (-leftChildAt.getMeasuredWidth())) { //  右滑极限
                    scrollTo(-leftChildAt.getWidth(), 0);
                    Log.d("diy", "onTouchEvent: 右滑极限 scrollX:"+scrollX+"distanceX:"+distanceX);
                    Log.d("diy", "leftChildAt.getMeasuredWidth()：" + leftChildAt.getMeasuredWidth() + ",leftChildAt.getWidth()：" + leftChildAt.getWidth());
                } else if (scrollX + distanceX > 0) { // 左滑极限
                    Log.d("diy", "onTouchEvent: 左滑极限 scrollX:"+scrollX+"distanceX:"+distanceX);
                    scrollTo(0, 0);
                } else { //正常
                    scrollBy((int) distanceX, 0); // 需要累计 scrollBy累计 scrollTo从左上角比较
                }
                downX = moveX;
                Log.d("diy", "onTouchEvent: scrollx:" + scrollX + ",distanceX:" + distanceX);
                break;
            case MotionEvent.ACTION_UP:
                int scrollStrarX = getScrollX(); //contantView滑动时起始位置
                if (scrollStrarX < -leftChildAt.getMeasuredWidth() / 2) {
//                    scrollTo(scrollEndX,0); // 没有缓冲滑动效果
                    slideMenuIsShow(true);
                } else {
//                    scrollTo(0,0);// 没有滑动效果
                    slideMenuIsShow(false);
                }

                //https://www.jianshu.com/p/a89062009b85
                //Scroller 实现滑动缓冲效果
                /*
                1、实例化Scroller
                2、调用startScroll(startX, startY, dx, dy, durationTime);模拟数据变化，接着调用invalidate(); 触发computeScroll()
                3、在computeScroll()里面复写真正让让模拟数据生效的代码,调用invalidate()
                 注意点：
                    scroller.computeScrollOffset()
                    computeScrollOffset()为true代表模拟数据还没有完成
                注意点：
                    scroller.getCurrX()当前时刻正模拟到的数据
                 */
                break;
        }
        return true;
    }

    private void slideMenuIsShow(boolean isShow) {
        int dx = 0;
        int scrollEndX = 0;
        int scrollStrarX = getScrollX();
        if (isShow) {
            slideMenuState = isShow;
            scrollEndX = -leftChildAt.getWidth();
            dx = scrollEndX - scrollStrarX;
        }else {
            slideMenuState = isShow;
            scrollEndX = 0;
            dx = scrollEndX - scrollStrarX;
        }
        mScroller.startScroll(scrollStrarX, 0, dx, 0, 800);
        invalidate();
    }

    // 模板代码
    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
            invalidate();
        }
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        leftChildAt = getChildAt(0);
        contentChildAt = getChildAt(1);
        mScroller = new Scroller(mContext);
        mImageButton = (ImageView) findViewById(R.id.iv_menu);
    }


    public boolean getSlideMenuState(){
        return this.slideMenuState;
    }

    public void setSlideMenuHidden(){
        slideMenuIsShow(false);
    }

    public void setSlideMenuShow(){
        slideMenuIsShow(true);
    }

}
